require("../less/home.less")
require("../lib/swiper/swiper-bundle.css")
require("../lib/iconfont/iconfont.css")

document.ready(function () {
    window.$utils.getfoot("home");
    
    /* 判断用户是否登录 */
    let userId = window.$utils.istongguo();

    /* 点击跳转页面 */
    document.querySelector(".training").addEventListener("click",function(){
        location.href = "Course.html";
    })
    document.querySelector(".run").addEventListener("click",function(){
        location.href = "run.html"
    })
    document.querySelector(".sports-date").addEventListener("click",function(){
        location.href = "sportDate.html"
    })
    document.querySelector("#sportsBange").addEventListener("click",function(){
        location.href = "Medal.html";
    })
    new Swiper('.swiper-container', {
        loop: true, // 循环模式选项
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },
        autoplay: {
            delay: 3000,
            stopOnLastSlide: true,
            disableOnInteraction: false,
        },
    })
    /* 封装函数 */
    getHome();
    function getHome() {
        /* 完成首页数据  配置ajax接口*/
        window.$http.get('/headPageInfo?userId=' + userId, function (res) {

            /* 判断是否成功 成功就渲染*/
            if (res.status === 0) {
                /* 把获取到的值渲染到网页 */
                document.querySelector('#ranking').textContent = res.data.rank;
                document.querySelector('#punchIn').textContent = res.data.punchIn;
                document.querySelector('#badge').textContent = res.data.insigniaNum;
                console.log(res)
            }

        })
    }

    /* 点击完成打卡 自动加1 */
    document.querySelector('#play').addEventListener('click', function () {
        /* 配置 ajax接口  */
        window.$http.get('/clockIn?userId=' + userId, function (data) {
            /* 如果成功 */
            if (data.status === 0) {
                let num = document.querySelector('#punchIn').textContent * 1
                num++;
                document.querySelector('#punchIn').textContent = num
                window.$utils.tips('打卡成功')
            } else {
                window.$utils.tips('已经打卡', '😒')
            }


        })
    })

    










})